package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Reference //不同服务间引入
    private IUserInfoService userInfoService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(Long id) {
        System.out.println(userInfoService.get(id));
        return "OK";
    }

    //查询手机号码时候已经注册
    @RequestMapping("/checkPhone")
    @ResponseBody
    public AjaxResult checkPhone(String phone) {
        return userInfoService.checkPhone(phone);
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public AjaxResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode){
            userInfoService.regist(phone, nickname, password, rpassword,verifyCode);
        return new AjaxResult();
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public AjaxResult userLogin(HttpServletResponse response, HttpSession session, String username, String password){
        //校验用户手机号码与密码,添加token
        String token = userInfoService.login(username, password);
        //添加cookie
        CookieUtil.addCookie(response,token);

        //共享用户对象
        UserInfo userInfo = userInfoService.getToken(token);
        session.setAttribute("userInfo",userInfo);
        return new AjaxResult();
    }
}
